<?php

App::uses("AttachAbstract", "Lib/attach");


/**
 * 
 */
class AttachFILE extends AttachAbstract {
	
	function __construct(&$attach) {
		$this->attach=$attach;
	}
	
	public function save() {
		try {
			$www_root = WWW_ROOT;
			// $estensione = pathinfo($this->attach->data["Attach"]["file_name"], PATHINFO_EXTENSION);
			$dest = $this->parsePath($this->attach->attachTipo->data['AttachTipo']['path']);
			if  (!file_exists($www_root.$dest)) {
				mkdir($www_root.$dest, 0777,1);
			}
			$filename = $this->attach->data["Attach"]["file_name"];
			if ($this->attach->eliminaFileTemporaneo()) {
				// $success = move_uploaded_file($this->attach->getFileTemporaneo(), WWW_ROOT.$dest."/".$filename);
				$success = copy($this->attach->getFileTemporaneo(), $www_root.$dest."/".$filename);
				if ($success) unlink($this->attach->getFileTemporaneo());
			} else {
				$success = copy($this->attach->getFileTemporaneo(), $www_root.$dest."/".$filename);
			}
			if (!$success) {
				throw new Exception(__("Errore in fase di copia dell'attach"));
			}
			chmod($www_root.$dest."/".$filename, 0777);
			$this->attach->path = $dest;
			$this->attach->filename = $filename;
			return $www_root.$dest."/".$filename;
		} catch (exception $e) {
			CakeLog::error($e->getMessage());
			throw new Exception(__("Errore in fase di copia dell'attach"));
		}
	}
	
	
	
	
	public function read() {
		try {
			$www_root = WWW_ROOT;
			// $estensione = pathinfo($this->attach->field('file_name'), PATHINFO_EXTENSION);
			// $dest = WWW_ROOT.$this->attach->attachTipo->data['AttachTipo']['path'].$this->attach->field('path');
			$dest = $this->parsePath($this->attach->attachTipo->data['AttachTipo']['path']);
			$filename = $this->attach->data["Attach"]["file_name"];
			if  (!file_exists($www_root.$dest."/".$filename)) {
				throw new NotFoundException(__("File inesistente."));
			} else {
				// print_r($dest."/".$filename);
				$this->attach->path = $dest;
				$this->attach->filename = $filename;
				return $www_root.$dest."/".$filename;
			}
		} catch (exception $e) {
			CakeLog::error($e->getMessage());
			throw new NotFoundException(__("Errore in fase di lettura dell'attach"));
		}
		
	}

	public function delete() {
		try {
			// pr($this->attach);die;
			$www_root = WWW_ROOT;
			$filename = $this->attach->data["Attach"]["file_name"];
			// $estensione = pathinfo($filename, PATHINFO_EXTENSION);
			$dest = $www_root.$this->parsePath($this->attach->attachTipo->data['AttachTipo']['path']);
			if  (file_exists($dest."/".$filename)) {
				$success = unlink($dest."/".$filename);
				if (!$success) {
					throw new Exception(__("Errore in fase di cancellazione dell'attach"));
				}
			}
		} catch (exception $e) {
			CakeLog::error($e->getMessage());
			throw new Exception(__("Errore in fase di cancellazione dell'attach"));
		}
	}
	
	private function parsePath($path) {
		/*
		 * questa funzione permette di parsare il path andando a prelefare delle varibili in sessione
		 * esempio: /tmp/{Auth.User.useranme}
		 * 
		 * */
		if (eregi("\{(path_dinamico)\}{1,1}", $path, $out)) {
				$strToReplace =  $out[0];
				$sessionKey = $out[1];
				$replace = $this->attach->data["Attach"]["path_dinamico"];
				if (empty($replace)) {
					throw new Exception(__("Campo path_dinamico non valorizzato."));
				}
				// print_r($path." ".$strToReplace." ".$sessionKey." ".$replace);die;
				return str_replace($strToReplace, $replace, $path);
		}
		return $path;
	}
	
}

?>